import UIKit

// 主视图控制者
class ViewController: UIViewController, GameCallback {
    @IBOutlet weak var mButtonStart: UIButton!  // "开始"按钮
    private var mGame: Game!                    // 游戏

    /**
     * "开始"按钮的单击响应方法。
     */
    @IBAction private func onButtonStartClicked() {
        let text = NSLocalizedString("IDS_RESTART", tableName: "Main", comment: "")
        mButtonStart.setTitle(text, for: .normal)

        mGame.start()
    }

    /**
     * 滑动手势的响应方法。
     * @param sender    事件发送者，即滑动手势
     */
    @IBAction private func onFling(_ sender: UISwipeGestureRecognizer) {
        mGame.onFling(sender.direction)
    }

    /**
     * 游戏结束事件的响应方法。
     * @param isWon 胜利则为 true，否则为 false
     */
    internal func onGameOver(_ isWon: Bool) {
        var text: String!
        if isWon {
            text = NSLocalizedString("IDS_WON", tableName: "Main", comment: "")
        } else {
            text = NSLocalizedString("IDS_LOST", tableName: "Main", comment: "")
        }
        let ok = NSLocalizedString("IDS_OK", tableName: "Main", comment: "")
        let messageBox = UIAlertController(title: nil, message: text, preferredStyle: .alert)
        messageBox.addAction(UIAlertAction(title: ok, style: .default))
        super.present(messageBox, animated: true)

        text = NSLocalizedString("IDS_START", tableName: "Main", comment: "")
        mButtonStart.setTitle(text, for: .normal)
    }

    /**
     * 视图加载事件的响应方法。
     */
    internal override func viewDidLoad() {
        super.viewDidLoad()

        // 禁用 translatesAutoresizingMaskIntoConstraints，否则控件约束将无效
        mButtonStart.translatesAutoresizingMaskIntoConstraints = false

        // 获取屏幕尺寸
        let screenWidth = UIScreen.main.bounds.width
        let screenHeight = UIScreen.main.bounds.height

        // 计算小格的尺寸
        let smallGridSize = screenWidth / (Const.SMALL_GRID_COUNT_PADDING * 2 + Const.SMALL_GRID_COUNT_GRID * CGFloat(Const.GAME_BOARD_GRID_COUNT_V))
        
        // 计算格的尺寸
        Const.GRID_SIZE = smallGridSize * Const.SMALL_GRID_COUNT_GRID
        
        // 计算按钮的尺寸
        let btnHeight = Const.SCALE_BUTTON_HEIGHT * smallGridSize
        
        // 计算游戏区域的尺寸和位置
        let gameBoardWidth = Const.GRID_SIZE * CGFloat(Const.GAME_BOARD_GRID_COUNT_H) + 1
        let gameBoardHeight = Const.GRID_SIZE * CGFloat(Const.GAME_BOARD_GRID_COUNT_V) + 1
        let gameBoardX = smallGridSize
        let gameBoardY = (screenHeight - gameBoardHeight - smallGridSize - smallGridSize - btnHeight) / 2
        
        // 创建游戏区域
        let gameBoard = GameBoard(frame: CGRect(x: gameBoardX, y: gameBoardY, width: gameBoardWidth, height: gameBoardHeight))
        view.addSubview(gameBoard)

        // 布局
        /* "开始"按钮 */
        let lcButtonStartWidth = NSLayoutConstraint(item: mButtonStart!, attribute: .width, relatedBy: .equal, toItem: gameBoard, attribute: .width, multiplier: 1.0, constant: 0.0)
        view.addConstraint(lcButtonStartWidth)

        let lcButtonStartHeight = NSLayoutConstraint(item: mButtonStart!, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: btnHeight)
        mButtonStart.addConstraint(lcButtonStartHeight)

        let lcButtonStartX = NSLayoutConstraint(item: mButtonStart!, attribute: .left, relatedBy: .equal, toItem: gameBoard, attribute: .left, multiplier: 1.0, constant: 0.0)
        view.addConstraint(lcButtonStartX)
        
        let lcButtonStartY = NSLayoutConstraint(item: mButtonStart!, attribute: .top, relatedBy: .equal, toItem: gameBoard, attribute: .bottom, multiplier: 1.0, constant: smallGridSize)
        view.addConstraint(lcButtonStartY)
        
        // 设置"开始"按钮的圆角半径
        mButtonStart.layer.cornerRadius = Const.GRID_SIZE * Const.SCALE_BUTTON_CORNER_RADIUS
        
        // 设置"开始"按钮的字体尺寸
        mButtonStart.titleLabel!.font = UIFont.systemFont(ofSize: smallGridSize * Const.SCALE_BUTTON_TEXT_SIZE)

        // 添加滑动手势支持
        let swipeGestureLeft = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.onFling))
        swipeGestureLeft.direction = .left
        view.addGestureRecognizer(swipeGestureLeft)
        
        let swipeGestureRight = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.onFling))
        swipeGestureRight.direction = .right
        view.addGestureRecognizer(swipeGestureRight)
        
        let swipeGestureUp = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.onFling))
        swipeGestureUp.direction = .up
        view.addGestureRecognizer(swipeGestureUp)
        
        let swipeGestureDown = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.onFling))
        swipeGestureDown.direction = .down
        view.addGestureRecognizer(swipeGestureDown)
        
        // 创建游戏
        mGame = Game(self, drawer: gameBoard.drawer, gameBoard: gameBoard)
    }
}
